import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { AuthService } from './auth.service';



@Injectable()
export class AuthGuard implements CanActivate {

  constructor(private authService: AuthService, private router: Router) { }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {

      return new Observable((observable)=>{

        this.authService.isLoggedIn().subscribe((res:any)=>{

          observable.next(true);

          // if(res.success){
          //   observable.next(true);
          // }else{
          //   observable.next(false);
          // }
        },(err:any)=>{
          this.router.navigate(['/login']);
          observable.next(false);
        });
      });
      
      
  }
}
